// components/called/called.js
const {
  request
} = require("../../utils/index.js")
import diaplayTime from '../../utils/timedisposal'
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    items: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    diaplayTime:"",
    word: true, //评论展开
    comments: [], //评论列表数据
    inputValue: "", //评论内容
  },

  lifetimes: {
    ready() {
      this.getpinglun()
      // this.diaplayTime = new diaplayTime()
      this.diaplayTime(this.data.items.createTime)
      // console.log(this.diaplayTime)
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 视频展示
    chooseshow: function (e) {
      var that = this;
      wx.previewMedia({
        current: 0,
        type: 'video',
        url: that.data.items.files[0].updUrl
      })
    },
    diaplayTime:function (dateTimeStamp) {
    let newT = dateTimeStamp.replace(/-/g, '/'); //ios时间格式转换时，要求2019/07/04
    var result;
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var curTime = new Date(newT); //后端返回的是秒数
    var diffValue = now - curTime;
    //console.log('dateTimeStamp',dateTimeStamp);
    //console.log('diffValue',diffValue);
    if (diffValue < 0) {
      return;
    }
    var monthC = diffValue / month;
    var weekC = diffValue / (7 * day);
    var dayC = diffValue / day;
    var hourC = diffValue / hour;
    var minC = diffValue / minute;
    if (monthC >= 1) {
      if (monthC <= 12)
        result = "" + parseInt(monthC) + "月前";
      else {
        result = "" + parseInt(monthC / 12) + "年前";
      }
    } else if (weekC >= 1) {
      result = "" + parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
      result = "" + parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
      result = "" + parseInt(hourC) + "小时前";
    } else if (minC >= 1) {
      result = "" + parseInt(minC) + "分钟前";
    } else {
      result = "刚刚";
    }
    this.setData({
      diaplayTime:result
    })
    return result;
  },
    called: function () {
      wx.navigateTo({
        url: '../calleddetails/calleddetails?atyId=' + this.data.items.atyId,
      })
    },
    //召集收藏增加
    zjshoucang:async function(e){
      let param={
       url:"hw/portal/user/collect/save",
       header:{
        'content-type': 'application/x-www-form-urlencoded', // 默认值
       },
       data:{
         "token":wx.getStorageSync('token'),
        "collectModule":"activity_call",
        "objId":this.data.items.atyId,
       },
       method:"POST"
      }
      let datainfo=await request(param);
      if(datainfo.status==100){
        this.setData({
          "items.isCollect":!this.data.items.isCollect
        })
        console.log(this.data.items.isCollect)
      }
    },
    //召集收藏取消
    zjcanceshoucang:async function(){
      let parm={
        url:"hw/portal/user/collect/del",
        header:{
         'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        data:{
         "token":wx.getStorageSync('token'),
         "collectModule":"activity_call",
         "objId":this.data.items.atyId,     //当前用户id
        },
        method:"POST"
       }
       let datainfo =await request(parm);
       if(datainfo.status==100){
         this.setData({
          "items.isCollect":!this.data.items.isCollect
         })
         console.log(this.data.items.isCollect)
       }
    },
    //评论展开或收起
    word: function () {
      this.setData({
        word: !this.data.word
      })
      console.log(this.data.word)
    },
    //评论
    async getpinglun() {
      var that = this;
      let par = {
        data: {
          cmtModule: 'activity_call', //需要查看评论对象
          // pageNum: 1, //页码
          // pageSize: 5, //每页条数
          objId: this.data.items.atyId, //需要查看评论对象的ID
        },
        url: 'hw/portal/comment/page?token=' + wx.getStorageSync('token'),
        method: "get"
      }
      let res = await request(par);
      if (res.status == 100) {
        this.setData({
          comments: res.data.list
        })
      }
    },
    //评论内容
    blur(e) {
      this.setData({
        inputValue: e.detail.value
      });
    },
    async enter() {
      let that = this;
      if (that.data.inputValue == "") {
        wx.showToast({
          title: '请填写评论内容',
          icon: "none",
        })
      } else {
        let param = {
          url: "hw/portal/comment/save",
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded', // 默认值
          },
          data: {
            "token": wx.getStorageSync('token'),
            cmtContent: that.data.inputValue,
            cmtModule: "activity_call",
            objId: this.data.items.atyId, //id
          }
        }
        let data = await request(param);
        let list=[];
        if(that.data.comments){
          list=[...that.data.comments, data.data]
        }else{
          list.push(data.data)
        }
        if (data.status == 100) {
          that.setData({
            inputValue: "",
            comments: list
          })
          wx.showToast({
            title: '发送成功',
            icon: "none",
          })
        }
      }
    },
  }
})